package com.beidouin.GnssMain.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.beidouin.GnssMain.adapter.SatelliteAdapter;
import com.beidouin.GnssMain.models.BeidouSatellite;
import com.beidouin.GnssMain.models.BeidouStatus;
import com.beidouin.GnssMain.R;

import java.util.ArrayList;

/**
 * Created by yuyang on 16/6/24.
 */
public class GpsListFragment extends Fragment {
    ListView mListView;
    SatelliteAdapter mAdapter;
    ArrayList<BeidouSatellite> mListData;
    // newInstance constructor for creating fragment with arguments
    public static GpsListFragment newInstance(int page, String title) {
        GpsListFragment fragmentFirst = new GpsListFragment();
        Bundle args = new Bundle();
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

    // Store instance variables based on arguments passed
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mListData = new ArrayList<BeidouSatellite>();
    }

    public void validateUI(){
        mAdapter.setData(BeidouStatus.getInstance().getSatellitesList());
        mAdapter.clearEmpty();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View parentView = inflater.inflate(R.layout.fragment_gps_list, container, false);
        mListView = (ListView) parentView.findViewById(R.id.satellite_lv);
        mAdapter = new SatelliteAdapter(getActivity());

        mAdapter.setData(BeidouStatus.getInstance().getSatellitesList());
        mListView.setAdapter(mAdapter);

        coordinateListViewWithSRL(mListView);
        mAdapter.clearEmpty();
        mAdapter.notifyDataSetChanged();
        return parentView;
    }


    /**
     * helper method, disable swipe refresh if list view is not on its top you
     * should call this on onActivityCreated method
     *
     * @param listView
       */
    protected void coordinateListViewWithSRL(final ListView listView) {
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            boolean isLastRow = false;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {


            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {

                    isLastRow = true;
                }
            }
        });
    }
}
